package com.yyy.dao;

import com.yyy.entity.Category;
import com.yyy.entity.Config;
import com.yyy.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：yyy
 * @Package：com.yyy.dao
 * @Project：HuTuBiill
 * @name：ConfigDao
 * @Date：2024/11/11 21:22
 * @Filename：ConfigDao
 * config类持久层
 */
public class ConfigDao {
    /*
    * 添加
    * */
    public void add(Config config){
        String sql="insert into config values(null,?,?)";

        try {
            Connection c= DBUtil.getConnection();
            PreparedStatement ps=c.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            ps.setString(1,config.key);
            ps.setString(2, config.value);
            ps.execute();
            ResultSet rs=ps.getGeneratedKeys();
            if(rs.next()){
                int id=rs.getInt(1);
                config.id=id;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    /*
    *
    * 删除
    * */

    public void delete(int id){
        try {
            String sql="delete from config where id="+id;
            Connection c= DBUtil.getConnection();
            Statement ps=c.createStatement();
            ps.execute(sql);


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /*
    *
    * 更新
    * */

    public void update (Config config){
        String sql="update config set key_=?,value_=? where id=?";
        try {
            Connection c= DBUtil.getConnection();
            PreparedStatement ps=c.prepareStatement(sql);
            ps.setString(1,config.key);
            ps.setString(2,config.value);
            ps.setInt(3,config.id);

            ps.execute();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    /*
    * 根据id查询
    * */

    public Config selectById(int id){
        Config config=null;
        try {
            String sql="select * from config where id="+id;
            Connection c= DBUtil.getConnection();
            Statement ps=c.createStatement();
            ResultSet rs=ps.executeQuery(sql);
            if (rs.next()){
                config=new Config();
                config.id=id;
                config.key=rs.getString("key_");
                config.value=rs.getString("value_");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return config;
    }

    /*
    * 通过key查询Config
    * */
    public Config selectByKey(String  key){
        Config config=null;
        try {
            String sql="select * from config where key_=?";
            Connection c= DBUtil.getConnection();
            PreparedStatement ps=c.prepareStatement(sql);
            ps.setString(1,key);

            ResultSet rs=ps.executeQuery();
            if (rs.next()){
                config=new Config();
                config.id=rs.getInt("id");
                config.key=key;
                config.value=rs.getString("value_");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return config;
    }

    /*
     * 分页查询
     * */
    public List<Config> list(int start, int count){
        List<Config> configs=new ArrayList<>();
        String sql="select * from config order by id desc limit ?,?";
        try {
            Connection connection=DBUtil.getConnection();
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setInt(1,start);
            ps.setInt(2,count);
            ResultSet rs= ps.executeQuery();
            while (rs.next()){
                Config config=new Config();
                int id=rs.getInt(1);
                String key=rs.getString("key_");
                String value=rs.getString("value_");
                config.id=id;
                config.key=key;
                config.value=value;
                configs.add(config);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return configs;
    }

    /*
    *
    * 查询全部
    * */

    public List<Config> list(){
        return list(0,Short.MAX_VALUE);
    }

    /*
    * 查询总笔数
    * */
    public int getTotal(){
        int total=0;
        try {
            String sql="select count(1) from config" ;
            Connection connection=DBUtil.getConnection();
            Statement ps= connection.createStatement();
            ResultSet resultSet = ps.executeQuery(sql);
            while (resultSet.next()){
                total=resultSet.getInt(1);
            }
            System.out.println("total:"+total);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return total;
    }



}
